//
//  ViewController.m
//  01-UITableViewQQ发现界面
//
//  Created by parkhome2 on 17/3/24.
//  Copyright © 2017年 parkhome2. All rights reserved.
//

#import "ViewController.h"
#import "QQModelData.h"
#import "FriendViewController.h"
#import "ScanViewController.h"

@interface ViewController ()<UITableViewDataSource,UITableViewDelegate>

/** 创建tableView */
@property (nonatomic, strong) UITableView *tableView;
/** 数据模型 */
@property (nonatomic, strong) NSArray *qqModel;

/** 数组 */
@property (nonatomic, strong) NSArray *targetList;

@end

@implementation ViewController

#pragma  mark  ******    懒加载
-(UITableView *)tableView{
    if (_tableView == nil) {
        _tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        _tableView.dataSource = self;
        _tableView.delegate = self;
        
        
        // 设置分割线内间距
        _tableView.separatorInset = UIEdgeInsetsMake(0, 10, 0, 0);
        // 设置分割线的颜色
//        _tableView.separatorColor = [UIColor redColor];
        // 设置每个cell间距线
//        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        
        _tableView.sectionFooterHeight = 30;
        _tableView.sectionHeaderHeight = 0;
        
        UIView *headView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 20)];
        _tableView.tableHeaderView = headView;
 

    }
    return _tableView;
}

-(NSArray *)qqModel{
    if (!_qqModel) {
        _qqModel = [QQModelData loadQQModelData];
        
    }
    return _qqModel;
}


#pragma  mark  ******    viewDidLoad

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    
    [self.view addSubview:self.tableView];
    
    self.title = @"发现";
    
    NSArray *section = @[@"FriendViewController"];
    NSArray *section2 = @[@"ScanViewController"];
    
    self.targetList = @[section,section2];

    
}

#pragma  mark  ******    UITableView数据源

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    
    return self.qqModel.count;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
    
    NSArray *templist = self.qqModel[section];
    
    return templist.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    static NSString *cellID = @"CELL";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellID];
    if (!cell) {
        cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:cellID];
    }

    QQModelData *qqdata = self.qqModel[indexPath.section][indexPath.row];
    
    cell.textLabel.text = qqdata.title;
    cell.imageView.image = [UIImage imageNamed:qqdata.icon];
    
    /** 
     添加小箭头
     UITableViewCellAccessoryNone,                                                      // don't show any accessory view
     UITableViewCellAccessoryDisclosureIndicator,                                       // regular chevron. doesn't track
     UITableViewCellAccessoryDetailDisclosureButton __TVOS_PROHIBITED,                 // info button w/ chevron. tracks
     UITableViewCellAccessoryCheckmark,                                                 // checkmark. doesn't track
     UITableViewCellAccessoryDetailButton NS_ENUM_AVAILABLE_IOS(7_0)  __TVOS_PROHIBITED // info button. tracks

     
     */
    
//    if (indexPath.section == 0) {
//        cell.accessoryView = [[UISwitch alloc] init];
//    }
    
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    
    /** 
     点击每个cell的样式，不变色
     UITableViewCellSelectionStyleNone,
     UITableViewCellSelectionStyleBlue,
     UITableViewCellSelectionStyleGray,
     UITableViewCellSelectionStyleDefault NS_ENUM_AVAILABLE_IOS(7_0)
     };

     */
//    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    return cell;
   
}


//-(NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section{
//    return @"hello";
//}

#pragma  mark  ******    UITableView代理

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    
    UIView *v = [[UIView alloc] initWithFrame:CGRectMake(0, 0, 0, 0)];
    
    v.backgroundColor = [UIColor greenColor];
    
    return v;
}


-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
    /** 点击每个cell的样式，出现动画，变色 */
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
//    
//    if (indexPath.section == 0 && indexPath.row == 0) {
//        FriendViewController *friend = [[FriendViewController alloc] init];
//        [self.navigationController pushViewController:friend animated:YES];
//    }
    
    NSString *targetName = self.targetList[indexPath.section][indexPath.row];
    
    UIViewController *vc = [[NSClassFromString(targetName) alloc] init];
    
    [self.navigationController pushViewController:vc animated:YES];
    
    NSLog(@"%ld",(long)indexPath.section);
}
@end
